package com.cxyd.modules.api.controller;

import com.cxyd.common.annotation.LogApiOperation;
import com.cxyd.common.utils.ApiResult;
import com.cxyd.modules.api.dto.RcsResult;
import com.cxyd.modules.wcs.dto.AgvCallbackDTO;
import com.cxyd.modules.wcs.dto.ReceiveTaskDTO;
import com.cxyd.modules.wcs.service.AgvCallbackService;
import com.cxyd.modules.wcs.service.ReceiveTaskService;
import org.springframework.web.bind.annotation.*;

import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class ApiController {
    private final ReceiveTaskService receiveTaskService;
    private final AgvCallbackService callbackService;

    public ApiController(ReceiveTaskService receiveTaskService, AgvCallbackService callbackService) {
        this.receiveTaskService = receiveTaskService;
        this.callbackService = callbackService;
    }

    @GetMapping("ping")
    public ApiResult ping() {
        return new ApiResult().success("当前时间:" + LocalTime.now());
    }

    @PostMapping("receive")
    @LogApiOperation("IWMS任务接收")
    public ApiResult receiveTask(@RequestBody ReceiveTaskDTO dto) {
        try {
            //1.校验数据格式
            final String requestPk = dto.getRequestPk();
            if (requestPk.length() == 0) {
                return new ApiResult().feedBack("requestPk不能为空!", "404");
            }
            final String contNo = dto.getContNo();
            if (contNo.length() == 0) {
                return new ApiResult().feedBack("料箱条码不能为空!", "404");
            }
            //2.校验业务数据
            Map<String, Object> params = new HashMap<>();
            params.put("requestPk", requestPk);
            params.put("contNo", contNo);
            final ReceiveTaskDTO oneByContNo = receiveTaskService.getOneByContNo(params);
            if (oneByContNo != null) {
//                1.任务存在需要判断是否为更新优先级
//                System.out.println("请求Pk[" + dto.getRequestPk() + "]旧PK[" + oneByContNo.getRequestPk() + "]");
                boolean flag = receiveTaskService.updateReceiveTaskByPrty(dto, oneByContNo);
                if (flag) {
                    return new ApiResult().feedBack(requestPk);
                } else {
                    return new ApiResult().feedBack("料箱[" + contNo + "]任务已存在!", requestPk);
                }
            }
            //2.将接口数据保存到[wcs_receive_task]表中
            receiveTaskService.save(dto);
            //反馈成功
            return new ApiResult().feedBack(requestPk);
        } catch (Exception ex) {
            return new ApiResult().feedBack(ex.getMessage(), dto.getRequestPk());
        }
    }

    @PostMapping("agvCallback")
    @LogApiOperation("CTU任务执行通知")
    public RcsResult agvCallback(@RequestBody AgvCallbackDTO dto) {
        try {
//            记录通知信息
            callbackService.save(dto);
//            调用callbackHandle处理业务
            callbackService.callbackHandle(dto);
        } catch (Exception ex) {
            return new RcsResult().error(dto.getReqCode(), ex.getMessage());
        }
        return new RcsResult().success(dto.getReqCode());
    }
}
